Python Indentation

Python uses whitespace (spaces and tabs) to define program blocks whereas other languages like C, C++ use braces ({}) to indicate blocks of codes for class, functions or flow control. The number of whitespaces (spaces and tabs) in the indentation is not fixed, but all statements within the block must be the indented same amount. In the following program, the block statements have no indentation.

Python statements without indentation

This is a program with singale space indentation.

Python single space indentation
This is a program with singale tab indentation.
Python single tab indentation example 
Here is another program with an indetation of a single space + a single tab.
Python single space and -tab indentation 
Python Coding Style
  • Use 4 spaces per indentation and no tabs.
  • Do not mix tabs and spaces. Tabs create confusion and it is recommended to use only spaces.
  • Maximum line length : 79 characters which help users with a small display.
  • Use blank lines to separate top-level function and class definitions and single blank line to separate methods definitions inside a class and larger blocks of code inside functions.
  • When possible, put inline comments (should be complete sentences).
  • Use spaces around expressions and statements.

No comments:

Post a Comment